﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Timers;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;
using System.Windows.Forms.DataVisualization.Charting;
using System.Xml;
using System.IO;
using PerfTabControl;


namespace test_project
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

            //PerfTabPage page = new PerfTabPage("Default", true);
            //PerfChart chart = new PerfChart();
            //page.AddChart(chart);
            tc1.TabPages.Add(new PerfTabPage("default",true));
            
            
            //LoadTemplateFile();
            LoadList();  
        }
        private void treeView1_DoubleClick(object sender, EventArgs e)
        {

        }
        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Level == 1)
            {
                PerformanceCounter[] counters;
                PerformanceCounterCategory pcc;


                TreeNode SelectedNode = e.Node;

                pcc = new PerformanceCounterCategory(SelectedNode.Text);

                string[] instances = pcc.GetInstanceNames();
                listBox1.Items.Clear();
                if (instances.Length > 0)
                {
                    foreach (string instance in instances)
                    {
                        listBox1.Items.Add(instance);
                    }
                    //listBox1.Items.Add("<All Instances>");
                    listBox1.SelectedIndex = 0;
                    counters = pcc.GetCounters(instances[0]);

                }
                
                if (!SelectedNode.IsExpanded)
                {
                    SelectedNode.Expand();
                }
            }

        }
        private void LoadList()
        {
                       
            TreeNode root = new TreeNode("Root - localhost");
            treeView1.Nodes.Add(root);

            PerformanceCounterCategory[] perfCounters = PerformanceCounterCategory.GetCategories();

            for (int i = 0; i < perfCounters.Length; i++)
            {
                // Add the category to the drop-down list
                TreeNode node = new TreeNode(perfCounters[i].CategoryName);
                root.Nodes.Add(node);
            }
            treeView1.Sort();
            treeView1.Nodes[0].Expand();
            foreach (TreeNode tnode in treeView1.Nodes[0].Nodes)
            {
                PerformanceCounter[] counters;
                PerformanceCounterCategory pcc;


                TreeNode SelectedNode = tnode;

                pcc = new PerformanceCounterCategory(SelectedNode.Text);

                string[] instances = pcc.GetInstanceNames();

                if (instances.Length > 0)
                {
                    counters = pcc.GetCounters(instances[0]);
                }
                else
                {
                    counters = pcc.GetCounters();

                }
                foreach (PerformanceCounter counter in counters)
                {
                    TreeNode node = new TreeNode(counter.CounterName);
                    SelectedNode.Nodes.Add(node);
                }
                ;
            }
        }
        private void btnaddtab_Click(object sender, EventArgs e)
        {
            tc1.TabPages.Add(new PerfTabPage("test!",true));
        }
        private void btnaddchart_Click(object sender, EventArgs e)
        {
            PerfChart cht = new PerfChart();
            PerfTabPage page =  (PerfTabPage)tc1.TabPages[tc1.SelectedIndex];
            page.AddChart(cht);
        }
        private void btnaddctr_Click(object sender, EventArgs e)
        {
            
            if (treeView1.SelectedNode.Level == 2) //counter selected
            {
                
                TreeNode CounterNode = treeView1.SelectedNode;
                TreeNode CatNode = treeView1.SelectedNode.Parent;
                PerformanceCounter perfcnt = new PerformanceCounter();
                PerfTabPage page = (PerfTabPage)tc1.TabPages[tc1.SelectedIndex];

                if (listBox1.Items.Count > 0)
                {
                    //instance = listBox1.SelectedItem.ToString();
                    foreach (string item in listBox1.SelectedItems)
                    {
                        Counter ctr = new Counter(new PerformanceCounter(CatNode.Text, CounterNode.Text, item));
                        page.AddCounterToSelected(ctr);
                    }
                }
                else
                {
                    Counter ctr = new Counter(new PerformanceCounter(CatNode.Text, CounterNode.Text));
                    page.AddCounterToSelected(ctr);
                }
                
                page.Start();
            }
            
        }

        private void btnLoadTemplate_Click(object sender, EventArgs e)
        {
            tc1.LoadTemplate();
        }

       
    }
}
